package com.maltys.greedy;

import java.util.ArrayList;
import java.util.List;

/**
 * @author malty
 * @link <a href="https://leetcode.cn/problems/partition-labels/">划分字母区间</a>
 */
public class PartitionLabels {
    public List<Integer> partitionLabels(String s) {
        int[] charMap = new int[60];
        char[] chars = s.toCharArray();

        for (int i = 0; i < s.length(); i++) {
            charMap[chars[i] - 'a'] = i;
        }
        
        List<Integer> res = new ArrayList<>();
        int preStart = 0;
        int maxIdx = charMap[s.charAt(0) - 'a'];
        for (int i = 0; i < s.length(); i++) {
            maxIdx = Math.max(maxIdx, charMap[chars[i] - 'a']);
            if (i == maxIdx) {
                res.add(maxIdx - preStart + 1);
                preStart = maxIdx + 1;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        PartitionLabels labels = new PartitionLabels();
        List<Integer> list = labels.partitionLabels("aeccbbbbdec");
        System.out.println(list);
    }
}
